package com.wtwd.device.aspect;


import com.wtwd.device.annotation.PaidService;
import com.wtwd.device.service.PaidServiceService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author zdl
 * @date 2022/4/2 10:47
 */
@Component
@Aspect
public class PaidServiceAspect {

    @Resource
    private PaidServiceService paidServiceService;



    @Pointcut("@annotation(com.wtwd.device.annotation.PaidService)")
    public void servicePoint() {
    }

    /**
     * 付费服务
     *
     * @param proceedingJoinPoint
     */
    @Around("servicePoint() && @annotation(paidService)")
    public Object handlerPaidService(ProceedingJoinPoint proceedingJoinPoint, PaidService paidService) {
        Object returnValue =null;
        try {
            String serviceName = paidService.serviceName();
            boolean intercept = paidServiceService.intercept(serviceName);
            if (intercept) {
                returnValue = proceedingJoinPoint.proceed();
                //目标方法执行后处理
                if (returnValue != null) {//调用成功,记录调用次数
                    paidServiceService.add(serviceName);
                }
            }

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }finally {
            return returnValue;
        }
    }
}
